package com.pig4cloud.pig.iot.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.iot.entity.IotDeviceCategoryEntity;
import com.pig4cloud.pig.iot.service.IotDeviceCategoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 物联网设备分类
 *
 * @author lty
 * @date 2024-11-05 09:38:11
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/iotDeviceCategory" )
@Tag(description = "iotDeviceCategory" , name = "物联网设备分类管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class IotDeviceCategoryController {

    private final  IotDeviceCategoryService iotDeviceCategoryService;


	/**
	 * 根据idList查询出所有 category
	 * @param
	 * @param ids idList 如果是notIn且idList没有传递则返回所有数据,如果是in且idList没有传递则返回空
	 *            isIn 为0表示not in 为1表示in
	 * @return
	 */
	@Operation(summary = "分页查询" , description = "分页查询" )
	@PostMapping("/listByIds/{isIn}" )
	@PreAuthorize("@pms.hasPermission('iot_iotDeviceCategory_view')" )
	public R getIotDeviceCategoryListByIds(@RequestBody Long[] ids,@PathVariable String isIn) {
		List<Long> longs = Arrays.asList(ids);
		LambdaQueryWrapper<IotDeviceCategoryEntity> wrapper = Wrappers.lambdaQuery();
		if(ids.length>0){
			if(isIn.equals("1")){
				wrapper.in(IotDeviceCategoryEntity::getId,longs);
			}else if(isIn.equals("0")){
				wrapper.notIn(IotDeviceCategoryEntity::getId,longs);
			}
		}else{
			if(isIn.equals("1")){
				return R.ok(new ArrayList<>());
			}
		}
		return R.ok(iotDeviceCategoryService.list(wrapper));
	}


    /**
     * 分页查询
     * @param page 分页对象
     * @param iotDeviceCategory 物联网设备分类
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('iot_iotDeviceCategory_view')" )
    public R getIotDeviceCategoryPage(@ParameterObject Page page, @ParameterObject IotDeviceCategoryEntity iotDeviceCategory) {
        LambdaQueryWrapper<IotDeviceCategoryEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StrUtil.isNotBlank(iotDeviceCategory.getCategoryName()),IotDeviceCategoryEntity::getCategoryName,iotDeviceCategory.getCategoryName());
		wrapper.eq(StrUtil.isNotBlank(iotDeviceCategory.getCategoryCode()),IotDeviceCategoryEntity::getCategoryCode,iotDeviceCategory.getCategoryCode());
        return R.ok(iotDeviceCategoryService.page(page, wrapper));
    }


    /**
     * 通过id查询物联网设备分类
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('iot_iotDeviceCategory_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(iotDeviceCategoryService.getById(id));
    }

    /**
     * 新增物联网设备分类
     * @param iotDeviceCategory 物联网设备分类
     * @return R
     */
    @Operation(summary = "新增物联网设备分类" , description = "新增物联网设备分类" )
    @SysLog("新增物联网设备分类" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('iot_iotDeviceCategory_add')" )
    public R save(@RequestBody IotDeviceCategoryEntity iotDeviceCategory) {
        return R.ok(iotDeviceCategoryService.save(iotDeviceCategory));
    }

    /**
     * 修改物联网设备分类
     * @param iotDeviceCategory 物联网设备分类
     * @return R
     */
    @Operation(summary = "修改物联网设备分类" , description = "修改物联网设备分类" )
    @SysLog("修改物联网设备分类" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('iot_iotDeviceCategory_edit')" )
    public R updateById(@RequestBody IotDeviceCategoryEntity iotDeviceCategory) {
        return R.ok(iotDeviceCategoryService.updateById(iotDeviceCategory));
    }

    /**
     * 通过id删除物联网设备分类
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除物联网设备分类" , description = "通过id删除物联网设备分类" )
    @SysLog("通过id删除物联网设备分类" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('iot_iotDeviceCategory_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(iotDeviceCategoryService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param iotDeviceCategory 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('iot_iotDeviceCategory_export')" )
    public List<IotDeviceCategoryEntity> export(IotDeviceCategoryEntity iotDeviceCategory,Long[] ids) {
        return iotDeviceCategoryService.list(Wrappers.lambdaQuery(iotDeviceCategory).in(ArrayUtil.isNotEmpty(ids), IotDeviceCategoryEntity::getId, ids));
    }
}